<?php

namespace app\common\repository\user;

use app\common\model\user\User as UserModel;
use app\common\model\user\UserProfile as UserProfileModel;
use app\common\model\user\UserTag as UserTagModel;
use app\common\model\user\UserSaying as UserSayingModel;

class U
{
    public function __construct(
        UserModel $userModel,
        UserProfileModel $userProfileModel,
        UserTagModel $userTagModel,
        UserSayingModel $userSayingModel
    )
    {
        $this->userModel = $userModel;
        $this->userProfileModel = $userProfileModel;
        $this->userTagModel = $userTagModel;
        $this->userSayingModel = $userSayingModel;
    }

    public function updateUserProfile($user_id, $update)
    {
        $this->userProfileModel
            ->save($update, ['user_id' => $user_id]);
    }

    public function updateUserTag($user_id, $tag)
    {
        UserTagModel::destroy(function ($query) use ($user_id) {
            $query->where('user_id', $user_id);
        });
        $this->userTagModel->saveAll(array_map(function ($v) use ($user_id) {
            return [
                'user_id' => $user_id,
                'name' => $v
            ];
        }, $tag));
    }

    public function updateUserSaying($user_id, $saying)
    {
        UserSayingModel::destroy(function ($query) use ($user_id) {
            $query->where('user_id', $user_id);
        });
        $this->userSayingModel
            ->save(['user_id' => $user_id, 'text' => $saying]);
    }
}
